home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1997 May / PC Plus Super CD Issue 127 (May 1997).iso / fixes / delp126 / sum / sum1.pas < prev   
Encoding:
Pascal/Delphi Source File  |  1997-01-19  |  2.1 KB  |  64 lines

  1. unit Sum1;
  2. { PC Plus sample Delphi program.
  3.   Illustrates variable declaration, simple maths and
  4.   string conversions }
  5.  
  6.   { The following code is all inserted automatically by Delphi }
  7. interface
  8.  
  9.   { These are the source code units available to this program }
  10. uses
  11.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  12.   Forms, Dialogs, StdCtrls;
  13.  
  14.   { Delphi declares the main form used in the program and the    }
  15.   { visual objects it contains - such as the button and Edit box }
  16. type
  17.   TForm1 = class(TForm)
  18.     Edit1: TEdit;
  19.     Button1: TButton;
  20.     procedure Button1Click(Sender: TObject);
  21.   private
  22.     { Private declarations }
  23.   public
  24.     { Public declarations }
  25.   end;
  26.  
  27. var
  28.   Form1: TForm1;
  29.  
  30. implementation
  31.  
  32. {$R *.DFM}
  33.  
  34.  { The procedure which is run when the Button1 object is clicked    }
  35.  { This is the only piece of code which I (rather than Delphi) have }
  36.  { written in this program.                                         }
  37. procedure TForm1.Button1Click(Sender: TObject);
  38. var
  39.    { First, I've declared two variables }
  40.   i : integer;
  41.   s : string;
  42.    { Now I begin the coding section     }
  43. begin
  44.    { It's good practice to initialise  variables at the outset    }
  45.   s := '10';
  46.    { The StrToInt function returns the string, 's', as an integer }
  47.    { I have assigned the integer value to the variable, 'i'       }
  48.   i := StrToInt( s ); { i = 10  }
  49.    { Now I've multiplied i by itself. The result can then be      }
  50.    { assigned back to the original variable, 'i'                  }
  51.   i := i * i;         { i = 100 }
  52.    { I want to display this result in the edit box, Edit1.        }
  53.    { But the contents of Edit1 (it's Text property) is a string   }
  54.    { Therefore I must convert i into a string representation.     }
  55.    { The IntToStr returns a string which I can then assign to     }
  56.    { the variable, 's'.                                           }
  57.   s := IntToStr( i );
  58.    { The Edit1's object's 'Text' property is assigned the value   }
  59.    { of the string, 's'. So '100' appears in the edit box.        }
  60.   Edit1.Text := s;
  61. end;
  62.  
  63. end.
  64.